package com.ap.core.jbehave.enquire.status;

import com.ap.core.cxf.jaxb.managertp.EnquireRTPStatus1Request;
import com.ap.core.cxf.jaxb.managertp.EnquireRTPStatus1Response;
import com.ap.core.cxf.jaxb.paymentdetails.TxnStatusType;
import com.ap.core.exception.BusinessFailureException;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.jbehave.confirmpayment.ConfirmPaymentSteps;
import com.ap.core.message.info.BaseMessageInfo;
import com.ap.core.message.service.CoreResponseInfo;
import com.ap.core.util.JaxbUtil;
import com.ap.generator.equire.rtpstatus.EnquireRTPStatusGeneratorBuilder;
import com.core.ap.wsdl.managertpservice_1.EnquireRTPStatus1Fault;
import com.core.ap.wsdl.managertpservice_1.ManageRTP1PortType;
import org.hamcrest.CoreMatchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Steps class for repeated confirm payment message
 */
public class EnquireOrderStatusSteps extends ConfirmPaymentSteps {
    private EnquireRTPStatus1Response enquireRTPStatus1Response;

    public EnquireOrderStatusSteps(StoryContext storyContext) {
        super(storyContext);
    }

    @Given("the Order is in a logical state of Authorised")
    public void givenTheOrderIsInALogicalStateOfAuthorised(){

    }

    @When("a Payment Status Request message is received from the Distributor")
    public void whenAPCoreReceivesTheDuplicateValidCONFIRMPAYMENTRequestFromOSB() throws Exception {
        enquireRTPStatus(submitRTPRequest.getRTPSenderInfo().getDistributorInfo().getDistributorId(),
       submitRTPRequest.getRTPSenderInfo().getDetailMerchantInfo().getMerchantInfo().getMerchantId(),
                                            retrieveRTPRequest.getRequestInfo().getApTransactionId(),
                                                    retrieveRTPRequest.getRequestInfo().getApTRId());
    }

    @When("the message passes all Structural and Business validation")
    public void whenTheMessagePassesAllStructuralAndBusinessValidation(){

    }

    @When("the message includes the APTransactionID of the Order")
    public void whenTheMessageIncludesTheAPTransactionIDOfTheOrder(){

    }

    @Then("return the Payment Response back to the Distributor")
    public void thenReturnThePaymentResponseBackToTheDistributor() {
        assertThat("enquireRTPStatus1Response is null", true, CoreMatchers.is(CoreMatchers.equalTo(enquireRTPStatus1Response != null)));
        assertThat("TxnStatus is not Authorised", true, CoreMatchers.is(CoreMatchers.equalTo(TxnStatusType.AUTHORISED.equals(enquireRTPStatus1Response.getPaymtDetails().getPaymentStatusInfo().getTxnStatus()))));
    }

    private void enquireRTPStatus(String distributorId, String merchantId, String apTransactionId, String apTRId) throws BusinessFailureException, EnquireRTPStatus1Fault {
        final EnquireRTPStatus1Request enquireRTPStatus1Request = EnquireRTPStatusGeneratorBuilder.getSubmitRTPTypeGenerator().build().generate();
        enquireRTPStatus1Request.getSenderInfo().setDistributorId(distributorId);
        enquireRTPStatus1Request.getSenderInfo().setMerchantId(merchantId);
        enquireRTPStatus1Request.getTransactionStatusEnq().setApTransactionId(apTransactionId);
        enquireRTPStatus1Request.getTransactionStatusEnq().setApTRId(apTRId);
        JaxbUtil.newInstance().javaToXML(EnquireRTPStatus1Request.class, enquireRTPStatus1Request);
        final ManageRTP1PortType manageRTP1PortType = factory.getBean(ManageRTP1PortType.class);
        enquireRTPStatus1Response = manageRTP1PortType.enquireRTPStatus1(enquireRTPStatus1Request);
        JaxbUtil.newInstance().javaToXML(EnquireRTPStatus1Response.class, enquireRTPStatus1Response);
    }
}
